import Mock from "mockjs";

// 模拟博客分类数据
Mock.mock("/api/blogtype", "get", {
  code: 0,
  msg: "",
  "data": [
    {
      "name": "JavaScript",
      "order": 1,
      "articleCount": 1,
      "id": "60f8ca03c7517f1c080b2075"
    }
  ]
})


// 分页获取博客
import qs from "querystring";
Mock.mock(/\/api\/blog(\?.+)?$/, "get", function (option) {
  const query = qs.parse(option.url);

  const data = {
    "total": 1,
    "rows": [
      {
        "toc": [
          {
            "name": "传统形式",
            "anchor": "传统形式",
            "level": 1,
            "children": []
          },
          {
            "name": "借用构造函数",
            "anchor": "借用构造函数",
            "level": 1,
            "children": []
          },
          {
            "name": "公有原型",
            "anchor": "公有原型",
            "level": 1,
            "children": []
          },
          {
            "name": "圣杯模式",
            "anchor": "圣杯模式",
            "level": 1,
            "children": []
          }
        ],
        "title": "继承发展史",
        "description": "es6之前的各种实现继承的方式，从传统的原型链和到后面被称为是继承最完美解决方案的圣杯模式",
        "thumb": "https://lgy-assets.oss-cn-shanghai.aliyuncs.com/img/2022-8-14-23-30-6-752-a9516.jpeg",
        "createDate": 1660491013752,
        "scanNumber": 27,
        "commentNumber": 1,
        "id": "62f9150515fbde19f05c8014",
        "category": {
          "name": "JavaScript",
          "order": 1,
          "articleCount": 1,
          "id": "60f8ca03c7517f1c080b2075"
        }
      }
    ]
  }
  return Mock.mock({
    code: 0,
    msg: "",
    data
  })
})


// 单个博客详情
Mock.mock(/\/api\/blog\/(.+)$/, "get", {
  code: 0,
  msg: "",
  data: {
    "toc": [
        {
            "name": "传统形式",
            "anchor": "传统形式",
            "level": 1,
            "children": []
        },
        {
            "name": "借用构造函数",
            "anchor": "借用构造函数",
            "level": 1,
            "children": []
        },
        {
            "name": "公有原型",
            "anchor": "公有原型",
            "level": 1,
            "children": []
        },
        {
            "name": "圣杯模式",
            "anchor": "圣杯模式",
            "level": 1,
            "children": []
        }
    ],
    "title": "继承发展史",
    "description": "es6之前的各种实现继承的方式，从传统的原型链和到后面被称为是继承最完美解决方案的圣杯模式",
    "thumb": "/static/upload/2022-8-14-23-30-6-752-a9516.jpeg",
    "createDate": 1660491013752,
    "htmlContent": "<p><img src=\"https://lgy-assets.oss-cn-shanghai.aliyuncs.com/img/image-20220814232207520.png\" contenteditable=\"false\"><br></p><h1 id=\"传统形式\">传统形式</h1><p>最早实现继承的方式就是通过原型链，但这种方式会形成一个很长的链条，从而继承了很多不需要的属性，所以很快就被废弃了</p><p><br></p><p>缺点：</p><ul><li><p>继承了太多不需要的属性</p></li></ul><div data-language=\"js\" class=\"toastui-editor-ww-code-block\"><pre><code data-language=\"js\">/* \n现在son用到的属性就只需要Person的lastName的话，这样也能继承过来，但是除了这个lastName其他原型上面的方法是不是也继承过来了\n*/\n\nPerson.prototype.lastName = \"Li\";\nfunction Person() {\n\n}\nconst person = new Person();\n\nFather.prototype = person;\nFather.prototype.sayHi = function () {\n    console.log(\"GY\");\n}\nfunction Father() {\n\n}\nconst father = new Father;\n\nSon.prototype = father;\nfunction Son() {\n\n}\nconst son = new Son()\n\n// \nson.lastName\t// Li\nson.sayHi()\t\t// GY </code></pre></div><p>示意图</p><p><br></p><p><img src=\"https://lgy-assets.oss-cn-shanghai.aliyuncs.com/img/202207311958472.png\" alt=\"image-20220731195800422\" contenteditable=\"false\"><br></p><h1 id=\"借用构造函数\">借用构造函数</h1><p>这种方式那就是有一个构造函数，一模一样的实现了我现在这个构造函数的功能，然后把这个构造函数拿过来用。严格上来说这中方式不是继承，但是呢也是继承发展的一环，所以也有必要记录一下</p><p><br></p><p>缺点：</p><ul><li><p>不能继承借用构造函数的原型</p></li><li><p>每次构造函数都需要多执行一个函数，浪费效率</p></li></ul><div data-language=\"js\" class=\"toastui-editor-ww-code-block\"><pre><code data-language=\"js\">function Person(name, sex, age) {\n    this.name = name;\n    this.sex = sex;\n    this.age = age;\n}\n\nfunction Student(name, sex, age, height, weight) {\n    Person.call(this, name, sex, age);\n    this.height = height;\n    this.weight = weight;\n}\n\nconst student = new Student(\"zhang\", \"female\", 17, 165, 45)\n\n// student\n{name: 'zhang', sex: 'female', age: 17, height: 165, weight: 45}</code></pre></div><h1 id=\"公有原型\">公有原型</h1><p>这种方式，直接把一个构造函数的原型改为另一个构造函数的原型，让两个构造函数的原型都指向同一个地方。</p><p><br></p><p>缺点：</p><ul><li><p>因为两个构造函数都指向同一个原型，所以我一个构造函数改变了原型上边的属性，也会影响到另一个构造函数</p></li></ul><div data-language=\"js\" class=\"toastui-editor-ww-code-block\"><pre><code data-language=\"js\">Person.prototype.lastName = \"Li\";\nfunction Person() {\n\n}\n\nSon.prototype = Person.prototype;\nfunction Son() {\n\n}\n\nconst son = new Son();\n\n// \nson.lastName\t// Li</code></pre></div><p>示意图</p><p><br></p><p><img src=\"https://lgy-assets.oss-cn-shanghai.aliyuncs.com/img/202207312238769.png\" alt=\"image-20220731223857591\" contenteditable=\"false\"><br></p><h1 id=\"圣杯模式\">圣杯模式</h1><p>因为发生公有原型的继承方法有问题之后呢，诞生了圣杯模式，也是通过公有原型的方式来实现继承，但是现在继承我不是直接将我的prototype改为父类的prototype，而是使用一个中间层。</p><ul><li><p>被称为最完美的解决方案</p></li></ul><div data-language=\"js\" class=\"toastui-editor-ww-code-block\"><pre><code data-language=\"js\">Person.prototype.lastName = \"Li\";\nfunction Person() {\n\n}\n\n// 这个构造函数F作为中间层，连接两个构造函数\nF.prototype = Person.prototype;\nfunction F() {\n\n}\n\nSon.prototype = new F;\nfunction Son() {\n\n}\n\nconst son = new Son();</code></pre></div><p>封装一个函数来实现这个继承方式s</p><div data-language=\"js\" class=\"toastui-editor-ww-code-block\"><pre><code data-language=\"js\">const inherit = (function () {\n    function F() { };\n    return function (Target, Origin) {\n        F.prototype = Origin.prototype;\n        Target.prototype = new F();\n    }\n})()</code></pre></div><p><img src=\"https://lgy-assets.oss-cn-shanghai.aliyuncs.com/img/202207312247873.svg\" alt=\"1\" contenteditable=\"false\"><br></p>",
    "scanNumber": 32,
    "commentNumber": 1,
    "id": "62f9150515fbde19f05c8014",
    "category": {
        "name": "JavaScript",
        "order": 1,
        "articleCount": 1,
        "id": "60f8ca03c7517f1c080b2075"
    }
}
})

// 提交评论
Mock.mock("/api/comment", "post", {
  code: 0,
  msg: "",
  data: {
    "id|1-10": 1,
    nickname: "@cname",
    content: "@csentence(30,150)",
    blog: {
      id: 1,
      title: "博客标题"
    },
    createDate: "@date('T')",
    avatar: "@image(250x250,  #000, #fff, Avatar)",
  }
})

// 获取分页评论
Mock.mock(/\/api\/comment\/?(\?.+)?$/, "get", function (option) {
  const query = qs.parse(option.url);
  return Mock.mock({
    code: 0,
    msg: "",
    data: {
      total: 0,
      "rows": [
      ]
    }
  })
})